package com.data.modules.smzh.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data.modules.gd.entity.GdProgramme;
import com.data.modules.smzh.entity.Devicebuild;
import com.data.modules.smzh.entity.Facedevice;
import com.data.modules.smzh.mapper.FacedeviceMapper;
import com.data.modules.smzh.service.IDevicebuildService;
import com.data.modules.smzh.service.IFacedeviceService;
import com.data.modules.smzh.vo.SmDeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: facedevice
 * @Author: data-boot
 * @Date:   2022-11-29
 * @Version: V1.0
 */
@Service
public class FacedeviceServiceImpl extends ServiceImpl<FacedeviceMapper, Facedevice> implements IFacedeviceService {

    @Autowired
    private FacedeviceMapper facedeviceMapper;
    @Autowired
    private IDevicebuildService iDevicebuildService;


    @Override
    @Transactional
    public Boolean addBindDevice(SmDeviceVo smDeviceVo) {

        Boolean flag = false;

        //{"serialNo":"18618367512","serialName":"测试","uniqueNo":"","type":0,"model":"STD-TEST"}

        // type =2 组装序列号

		/* if (ctx.request.body.type === 2) {
			 ctx.request.body.serialNo = _appid.app_id + ctx.request.body.uniqueNo + ctx.request.body.serialNo;
		 }
                const device = await ctx.service.deviceManage.findDeviceBySerialNo(ctx.request.body.serialNo);
*/
        //通过序列号查询是否存在  存在 则失败

        //组装相关数据
	/*	  const sql = 'REPLACE into faceDevice (serial_no,app_id,address,threshold,push_url, model,device_type, update_time,create_time) VALUES (?)';
            const values = [];
            const params = [];
		 values.push(_serialNo);
		 values.push(appId);
		 values.push(_serialName);
		 values.push(82);
		 values.push('https://www.tll-tech.com/face-server/api/v1.0/putLog');

		  if (uniqueNo !== '') {
                const obj = {
                    serial_no: _serialNo,
                    building_no: appId + uniqueNo,
                    type: _type,
                };
                await conn.insert('deviceBuild', obj);
            }
*/

        String url = "https://www.tll-tech.com/face-server/api/v1.0/putLog";
        Facedevice facedevice = new Facedevice();
        BeanUtil.copyProperties(smDeviceVo,facedevice);
        facedevice.setAddress(smDeviceVo.getSerialName());
        facedevice.setThreshold(82);
        facedevice.setPushUrl(url);
        facedevice.setDeviceType(smDeviceVo.getType());
        facedevice.setCreateTime(new Date());

        int i = facedeviceMapper.insert(facedevice);
        if(i == 1){

            if(ObjectUtil.isNotEmpty(facedevice.getSerialNo())){
                Devicebuild devicebuild = new Devicebuild();
                devicebuild.setBuildingNo(facedevice.getSerialNo());
                devicebuild.setSerialNo(facedevice.getSerialNo());
                devicebuild.setType(smDeviceVo.getType());
                devicebuild.setCreateTime(new Date());

                iDevicebuildService.save(devicebuild);
            }

            flag = true;
        }

        return flag;
    }

    /**
     * 编辑设备管理
     * @param smDeviceVo
     * @return
     */
    @Override
    @Transactional
    public void updateBindDeviceInfo(SmDeviceVo smDeviceVo) {

        // 设备编号不会变更，appId 不会变更，只会 更新设备基本信息

        /**
         * 通过设备号 查询是否已经被绑定 ,  存在  判断 序列号是否为空， 是 则删除。 否则做更新处理
         *   设备号 未绑定情况，序列号不为空，直接进行绑定
         */
        Facedevice facedevice = new Facedevice();
        BeanUtil.copyProperties(smDeviceVo,facedevice);

        this.update(new Facedevice().setAddress(smDeviceVo.getSerialName()).setModel(smDeviceVo.getModel()),
                new UpdateWrapper<Facedevice>().lambda().eq(Facedevice::getAppId,facedevice.getAppId())
        .eq(Facedevice::getSerialNo,facedevice.getSerialNo()));

        //facedevice.getSerialNo()

        int i = iDevicebuildService.lambdaQuery().eq(Devicebuild::getSerialNo,facedevice.getSerialNo()).list().size();

        if(i ==0){
            if(ObjectUtil.isNotEmpty(smDeviceVo.getUniqueNo())){

                Devicebuild devicebuild = new Devicebuild();
                devicebuild.setType(smDeviceVo.getType());
                devicebuild.setSerialNo(smDeviceVo.getSerialNo());
                devicebuild.setBuildingNo(smDeviceVo.getAppId()+smDeviceVo.getUniqueNo());
                iDevicebuildService.save(devicebuild);
            }
        }else{
            if(ObjectUtil.isNotEmpty(smDeviceVo.getUniqueNo())){

                this.iDevicebuildService.update(new Devicebuild().setBuildingNo(smDeviceVo.getAppId()+smDeviceVo.getUniqueNo()).setType(smDeviceVo.getType()),
                        new UpdateWrapper<Devicebuild>().lambda().eq(Devicebuild::getSerialNo,smDeviceVo.getSerialNo()));
            }else{

                iDevicebuildService.deleteBySerialNo(smDeviceVo.getSerialNo());
            }
        }

    }
}
